package com.it.test;

import java.sql.*;

/**
 * 数据库查询工具，用于查看it_machine_activation表的结构和数据
 */
public class DatabaseQueryTool {
    // 数据库连接信息
    private static final String URL = "jdbc:mysql://8.130.54.78:3306/it?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8";
    private static final String USERNAME = "root";
    private static final String PASSWORD = "wukong888";
    private static final String TABLE_NAME = "it_machine_activation";

    public static void main(String[] args) {
        Connection conn = null;
        try {
            // 加载数据库驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            
            // 建立数据库连接
            System.out.println("正在连接数据库...");
            conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
            System.out.println("数据库连接成功！\n");
            
            // 查看表结构
            System.out.println("===== 表结构 =====");
            showTableStructure(conn);
            
            // 查看表数据
            System.out.println("\n===== 表数据 =====");
            showTableData(conn);
            
        } catch (ClassNotFoundException e) {
            System.out.println("找不到数据库驱动类！" + e.getMessage());
        } catch (SQLException e) {
            System.out.println("数据库操作出错！" + e.getMessage());
        } finally {
            // 关闭数据库连接
            if (conn != null) {
                try {
                    conn.close();
                    System.out.println("\n数据库连接已关闭。");
                } catch (SQLException e) {
                    System.out.println("关闭数据库连接出错！" + e.getMessage());
                }
            }
        }
    }

    /**
     * 查看表结构
     */
    private static void showTableStructure(Connection conn) throws SQLException {
        Statement stmt = null;
        ResultSet rs = null;
        try {
            stmt = conn.createStatement();
            rs = stmt.executeQuery("DESC " + TABLE_NAME);
            
            // 打印列信息
            System.out.printf("%-20s %-20s %-10s %-10s %-10s %-10s\n",
                    "字段名", "类型", "是否为空", "键", "默认值", "额外信息");
            while (rs.next()) {
                String field = rs.getString(1);
                String type = rs.getString(2);
                String nullAble = rs.getString(3);
                String key = rs.getString(4);
                String defaultValue = rs.getString(5);
                String extra = rs.getString(6);
                
                System.out.printf("%-20s %-20s %-10s %-10s %-10s %-10s\n",
                        field, type, nullAble, key, defaultValue != null ? defaultValue : "", extra);
            }
        } finally {
            if (rs != null) rs.close();
            if (stmt != null) stmt.close();
        }
    }

    /**
     * 查看表数据
     */
    private static void showTableData(Connection conn) throws SQLException {
        Statement stmt = null;
        ResultSet rs = null;
        try {
            stmt = conn.createStatement();
            // 查询前10条数据
            rs = stmt.executeQuery("SELECT * FROM " + TABLE_NAME + " LIMIT 10");
            
            ResultSetMetaData metaData = rs.getMetaData();
            int columnCount = metaData.getColumnCount();
            
            // 打印列名
            for (int i = 1; i <= columnCount; i++) {
                System.out.printf("%-15s", metaData.getColumnName(i));
            }
            System.out.println();
            
            // 打印分隔线
            for (int i = 1; i <= columnCount; i++) {
                System.out.printf("%-15s", "---------------");
            }
            System.out.println();
            
            // 打印数据
            if (!rs.next()) {
                System.out.println("表中没有数据。");
                return;
            }
            
            do {
                for (int i = 1; i <= columnCount; i++) {
                    String value = rs.getString(i);
                    System.out.printf("%-15s", value != null ? value : "NULL");
                }
                System.out.println();
            } while (rs.next());
        } finally {
            if (rs != null) rs.close();
            if (stmt != null) stmt.close();
        }
    }
}